﻿@model Cxuu.Models.ExtMember
@{
    ViewData["Title"] = "保存";
}
<div class="content-top-nav">
    <div class="layui-row">
        <div class="layui-col-md11">
            <h3 style="color:#444">
                <i class="layui-icon layui-icon-home"></i>
                用户编辑
            </h3>
        </div>
        <div class="layui-col-md1" style="text-align:right;">
            <button class="layui-btn layui-btn-sm" onclick="loadMain('/Admin/User/')">返回列表</button>
        </div>
    </div>
    <hr>
</div>

<div class="layui-fluid" style="margin-bottom:50px;">
    <div class="layui-form">
        <form class="layui-form" action="" lay-filter="FormEdit" enctype="multipart/form-data">
            <div asp-validation-summary="ModelOnly" class="text-danger"></div>

            <div class="layui-form-item">
                <label asp-for="Name" class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <input type="text" asp-for="Name" lay-verify="required" lay-reqtext="姓名不能为空" placeholder="请输入姓名" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label asp-for="Duties" class="layui-form-label"></label>
                <div class="layui-input-inline" style="width:400px;">
                    <input type="text" asp-for="Duties" lay-verify="required" lay-reqtext="负责工作不能为空" placeholder="请输入负责工作" value="" class="layui-input">
                </div>
            </div>

            <div class="layui-form-item">
                <label asp-for="Photo" class="layui-form-label"></label>
                <div class="layui-input-inline"  style="width:400px;">
                    <input type="text" asp-for="Photo" id="Photo" value="" class="layui-input">
                    <button type="button" class="layui-btn" id="upload">
                      <i class="layui-icon">&#xe67c;</i>上传图片
                    </button>
                    <div style="width: 95px;">
                        <div class="layui-progress layui-progress-big" lay-showpercent="yes" lay-filter="upload-att">
                            <div class="layui-progress-bar" lay-percent=""></div>
                        </div>
                    </div>
                </div>                
            </div>

            <div class="layui-form-item">
                <label asp-for="Duty" class="layui-form-label"></label>
                <div class="layui-input-inline" style="width:300px;">
                    <input type="text" asp-for="Duty" lay-verify="required" lay-reqtext="职务不能为空" placeholder="请输入职务" value="" class="layui-input">
                </div>
            </div>
            <div class="layui-form-item">
                <label asp-for="Sort" class="layui-form-label"></label>
                <div class="layui-input-inline" style="width:50px;">
                    <input type="text" asp-for="Sort" placeholder="请输入人物排序" value="" class="layui-input">
                </div>
                 <div class="layui-form-mid layui-word-aux">输入纯数字，如：2</div>
            </div>

            <div class="layui-form-item">
                <label asp-for="Status" class="layui-form-label"></label>
                <div class="layui-input-inline">
                    <input asp-for="Status" type="radio" value="1" title="启用" checked>
                    <input asp-for="Status" type="radio" value="0" title="禁用">
                </div>
            </div>             
            <div class="layui-form-item">
                <div class="layui-input-block">
                    <button class="layui-btn" lay-submit lay-filter="saveBtn" id="saveBtn">@ViewData["Title"]</button>
                </div>
            </div>
        </form>
    </div>
</div>
<script>
    layui.use(['form', 'table', 'upload', 'jquery'], function () {
        var form = layui.form, layer = layui.layer, $ = layui.jquery,upload = layui.upload,element = layui.element;

        form.render();

        var editId = @ViewBag.id+1;


var uploadInst = upload.render({
            elem: '#upload'
            , url: '/Admin/Upload/Upload?type=layuiImg&source=Member&uid=@ViewBag.uid'
            , before: function (obj) {
                element.progress('upload-att', '0%'); //进度条复位
                layer.msg('上传中', { icon: 16, time: 0 });
            }
            , done: function (res) {
                if (res.status == 1) {
                     $('#Photo').val(res.data.url);
                    layer.msg(res.info);
                } else {
                    layer.msg("上传失败！");
                }
            }
            //进度条
            , progress: function (n, elem, e) {
                element.progress('upload-att', n + '%');
                if (n == 100) {
                    layer.msg('上传完毕', { icon: 1 });
                }
            }
        });


        if (editId > 1) {
            $.getJSON("/Admin/ExtMember/AddOrEdit?id=@ViewBag.id&edit=1", function (res) {
                if (res.status == 1) {
                    form.val('FormEdit', {
                        Name: res.data.name,
                        Duties: res.data.duties,
                        Photo: res.data.photo,
                        Duty: res.data.duty,
                        Sort: res.data.sort,
                        Status: res.data.status,
                    });
                    if (res.data.status)
                        $('input[name="Status"][value="true"]').prop('checked', 'checked');
                    else
                        $('input[name="Status"][value="false"]').prop('checked', 'checked');
                    form.render('radio');
                }
                //console.log("json", res+"abc");
            })
        } else {
            $('input[name="Status"][value="true"]').prop('checked', 'checked');
            form.render('radio');
         }

        var dataPost = function (url,data) {
            $.post(url, data, function (res) {
                if (res.status == 1) {
                    layer.msg(res.msg, { icon: 1, time: 1000 }, function () {
                        loadMain('/Admin/ExtMember/Index/');
                    });
                } else {
                    layer.msg(res.msg, { icon: 2, anim: 6, time: 5000 });
                }
            });
        }

        //监听提交
       // form.on('submit(saveBtn)', function (data) {
        $('#saveBtn').click(function () {
            var post = form.val('FormEdit');
            if (editId == 1)
                dataPost('/Admin/ExtMember/CreatePost', post);
             else
                dataPost('/Admin/ExtMember/EditPost?id=@ViewBag.id', post);
            return false;
        });


    });

</script>